import { EV_LOGIN } from "@/constant/event";
import { AppData } from "./app";
import { Router } from "./router";

export class Account {
  static get isLogin() {
    return !!AppData.userData.telephone;
  }

  static login(callback: (isLogin: boolean) => void, cancel?: () => void) {
    Router.navigateTo("/pages/login/login", () => {
      uni.$once(EV_LOGIN, (e: { type: "cancel" }) => {
        console.info(`触发事件：${EV_LOGIN}`, e);
        e.type === "cancel" ? cancel?.() : callback(true);
      });
    });
  }

  static checkLogin(callback: (isLogin: boolean) => void, cancel?: () => void) {
    Account.isLogin ? callback(false) : Account.login(callback, cancel);
  }
}
